var progress = new CustomProgressBar();

var scripts = [
    '../src/ObjectContainer.js',
    '../src/EventSet.js',
    '../src/DataCookieWriter.js',
    '../src/AsyncTask.js',
    '../src/DataStorageWriter',
    '../src/DataStorage.js',
    '../src/GameLoop.js',
    '../src/ScopeRouter.js',
    '../src/ResourceManager.js',
    '../src/ResourceLoader.js',
    '../src/SwitchMap.js',
    '../src/Screen.js',
    '../src/KeyboardInput.js',
    '../src/MouseInput.js'
];

window.onload = function() {
    var content = "";
    
    function go(i, callb) {
        loadScriptContent(scripts[i], function (res) {
            content += res;
            callb(i + 1);
        });
    }
    
    function loadScriptContent(src, callback) {
        var xmlhttp;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = (function() {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                callback.call(this, xmlhttp.responseText);
            }
        }).bind(this);
        xmlhttp.open("GET", src, true);
        xmlhttp.send();
    };
    
    (function load(i) {
        if(i === scripts.length) {
            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.innerHTML = content;
            head.appendChild(script);
            return;
        };
        go(i, load);
        progress.update(progress.current+1);
    })(0);
};